package boogui.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import boogui.db.BooguiDAO;
import boogui.db.IBooguiDAO;

public class MovieSeatDAO implements IMovieSeatDAO {

	private String insertSeatSQL = "insert into seatinfo values()";
	private String selectSeatSQL = "select * from seatinfo where showno = ? order by showno desc";
	private IBooguiDAO bd = null;

	public MovieSeatDAO() {
		bd = new BooguiDAO();
	}

	@Override
	public boolean insertSeat(S_MovieSeatDialog m) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;

		try {
			conn = bd.openDatabase();
			pstmt = conn.prepareStatement(insertSeatSQL);
			pstmt.executeUpdate();
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bd.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

	@Override
	public List<String> selectSeat(int showno) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<String> result = new ArrayList<>();

		try {
			conn = bd.openDatabase();
			pstmt = conn.prepareStatement(selectSeatSQL);
			pstmt.setInt(1, showno);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				for (int i = 2; i <= 181; i++) {
					result.add(rs.getString(i));
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bd.closeDatabase(conn, pstmt, rs);
		}
		return result;
	}
	@Override
	public boolean updateSeat(String sql) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;		
		try {
			conn = bd.openDatabase();
			pstmt = conn.prepareStatement(sql);
			pstmt.executeUpdate();
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			bd.closeDatabase(conn, pstmt, null);
		}
		return result;
	}

}